package com.education.welco.controller.teacherapp;

import com.education.common.pojo.AjaxResult;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtil;
import com.education.utils.BaiduAipContentCensor;
import com.education.welco.pojo.SAnswering;
import com.education.welco.pojo.SCatalogMenu;
import com.education.welco.service.ISAnsweringCommentService;
import com.education.welco.service.ISAnsweringService;
import com.education.welco.service.ISCatalogMenuService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: liftsail
 * @Date: 2022/11/29 16:12
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/apptea/Answer")
public class AppTeaAnswerController {

    @Autowired
    private BaiduAipContentCensor baiduAipContentCensor;
    @Autowired
    private ISAnsweringService answeringService;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private ISAnsweringCommentService sAnsweringCommentService;

    //课堂详情-课程考评-课堂答疑-列表
    @GetMapping("/list")
    @ApiOperation(value = "查课程下所有二级目录下的答疑", httpMethod = "GET")
    @ApiImplicitParam(name = "lessonId", value = "课程ID", dataType = "Integer")
    public AjaxResult sAnsweringList(@RequestParam(value = "lessonId") Integer lessonId) {
        SAnswering sAnswering = new SAnswering();
        sAnswering.setTopCatalogId(lessonId);
        List<SAnswering> sAnsweringList = answeringService.selectSAnsweringStuList(sAnswering);
        for (SAnswering answering : sAnsweringList) {
            //搜索答疑的讨论数量
            List<Integer> countEachAnswerTalkNum = sAnsweringCommentService.countEachAnswerTalkPeopleNum(answering.getId());
            answering.setTotal(countEachAnswerTalkNum.size());
        }
        return AjaxResult.success(sAnsweringList);
    }

    //todo 新增出现目录问题
    /**
     * 课堂详情-课堂答疑-添加-有效时间
     *
     * @param answering
     * @return
     * @throws ParseException
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(SAnswering answering) throws ParseException {
        if (answering.getTopCatalogId() != null && answering.getUserId() != null && answering.getEndTimeString() != null) {
            //设置创建日期
            answering.setCreateTime(new Date());
            //时间区间处理
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isBlank(answering.getAttendTimeString())) {
                answering.setAttendTime(DateUtil.getInialDateTime());
            } else {
                answering.setAttendTime(format.parse(answering.getAttendTimeString()));
            }
            answering.setEndTime(format.parse(answering.getEndTimeString()));
            //检测problemTitle是否符合规范
            if (org.apache.commons.lang3.StringUtils.isNotBlank(answering.getProblemTitle())) {
                int i = baiduAipContentCensor.AipText(answering.getProblemTitle());
                if (i == 2) {
                    return new ApiReturnObject("201", "您新增的课堂答疑中出现不当言论,请检查后再次提交", "您新增的课堂答疑中出现不当言论,请检查后再次提交");
                }
            }
            answeringService.insertSAnswering(answering);
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少catalogId", "");
    }

    /**
     * 课堂详情-课程考评-课堂答疑详情之主题内容
     * @param answerId
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查找答疑详情", httpMethod = "GET")
    public AjaxResult sAnsweringdetail(@RequestParam(value = "answerId") Integer answerId) {
        //查答疑的数据(答疑主题、老师名称、创建时间)
        SAnswering sAnswering = answeringService.selectSAnsweringById(answerId);
        //搜索答疑的讨论人数量
        List<Integer> countEachAnswerTalkNum = sAnsweringCommentService.countEachAnswerTalkPeopleNum(sAnswering.getId());
        //Integer countEachAnswerTalkNum = sAnsweringCommentService.countEachAnswerTalkNum(sAnswering.getId());
        sAnswering.setTotal(countEachAnswerTalkNum.size());
        return AjaxResult.success(sAnswering);
    }

    /**
     * 课堂详情-课程考评-课堂答疑详情之评论内容
     * @param answerId
     * @param userId
     * @param type
     * @return
     */
    @GetMapping("/detailComment")
    @ApiOperation(value = "查找答疑详情评论", httpMethod = "GET")
    @ApiImplicitParam(name = "type", value = "最热or最新- 1为最新2为最热", dataType = "String")
    public AjaxResult sAnsweringDetailComment(@RequestParam(value = "answerId") String answerId,
                                              @RequestParam(value = "userId") String userId,
                                              @RequestParam(value = "type") String type) {
        AjaxResult ajaxResult = sAnsweringCommentService.selectCommentListById(answerId, userId, type);
        //答疑讨论总数量
        Integer countEachAnswerTalkNum = sAnsweringCommentService.countEachAnswerTalkNum(Integer.valueOf(answerId));
        ajaxResult.put("countEachAnswerTalkNum", countEachAnswerTalkNum);
        return ajaxResult;
    }

    /**
     * 课堂详情-课程考评-课堂答疑详情之评论内容-置顶|取消置顶(答疑讨论)是否置顶 0 未置顶 1 已置顶
     * @param cPlId
     * @param stick
     * @return
     */
    @GetMapping("/stick")
    @ApiOperation("置顶|取消置顶(答疑讨论)")
    @ApiImplicitParam(name = "stick", value = "是否置顶 0 未置顶 1 已置顶", dataType = "Integer")
    public ApiReturnObject stickSave(Integer cPlId, Integer stick) {
        sAnsweringCommentService.stickSAnsweringCommentForTeacher(cPlId, stick);
        String msg = "置顶成功！";
        if (stick == 0) {
            msg = "取消置顶成功！";
        }
        return new ApiReturnObject("200", msg, stick);
    }

    //课堂详情-课程考评-课堂答疑详情之评论内容-评论点赞 参照app学生端/api/welco/app/sAnswering/commentLike

    //课堂详情-课程考评-课堂答疑详情之评论内容-评论举报 参照app学生端/api/welco/app/sAnswering/commentReport

    //课堂详情-课程考评-课堂答疑详情之评论内容-评论新增 参照app学生端/api/welco/app/sAnswering/commentAdd

    public List<SCatalogMenu> selectTwoList(Integer lessonId) {
        SCatalogMenu sCatalogMenu = new SCatalogMenu();
        //查询出第0级列表
        sCatalogMenu.setParentId(0);
        sCatalogMenu.setId(lessonId);
        //第0级
        SCatalogMenu list = catalogMenuService.selectSCatalogMenuList2(sCatalogMenu);
        //一级以及下面子列表
        return listSon2(list);
    }

    /**
     * 查询目录列表
     * @param cList
     * @return
     */
    public List<SCatalogMenu> listSon2(SCatalogMenu cList) {
        //根据课程id查询出一级目录列表jrCatalogs
        List<SCatalogMenu> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId(cList.getId());
        if (jrCatalogs != null && jrCatalogs.size() > 0) {
            //设置子级集合
            //cList.setChildren(listSon(jrCatalogs));
            return listSon(jrCatalogs);
        }
        return null;
    }

    public List<SCatalogMenu> listSon(List<SCatalogMenu> cList) {
        List<SCatalogMenu> twoSCatalogMenu = new ArrayList<>();
        for (SCatalogMenu catalogMenu : cList) {
            //根据第一级的id查询出二级列表
            List<SCatalogMenu> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId(catalogMenu.getId());
            //二级列表汇总
            twoSCatalogMenu.addAll(jrCatalogs);
            if (jrCatalogs != null && jrCatalogs.size() > 0) {
                //设置子级集合
                //catalogMenu.setChildren(listSon(jrCatalogs));
                //catalogMenu.setChildren(jrCatalogs);
            }
        }
        return twoSCatalogMenu;
    }

}
